package com.zhou.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.zhou.dto.Parameter;
import com.zhou.dto.Result;
import com.zhou.entity.Course;
import com.zhou.entity.CultivationPlan;
import com.zhou.service.CultivationPlanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author 周贵有
 * @date 2021/3/27
 */
@RestController
@RequestMapping("cultivationPlan")
public class CultivationPlanController {

    @Autowired
    private CultivationPlanService cultivationPlanService;

    @RequestMapping("findByMajor")
    public Result findByMajor(@RequestBody Parameter data){
        List<CultivationPlan> byMajor = cultivationPlanService.findByMajor((Integer) data.getData());
        // 根据开课时间，对获得的list进行分组
        Map<String, List<CultivationPlan>> groupBy = byMajor.stream().collect(Collectors.groupingBy(CultivationPlan::getStart_time));

        return Result.SUCCESS.setNewData(groupBy);
    }

    @RequestMapping("addCultivationPlan")
    public Result addCultivationPlan(@RequestBody String data){

        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 使用map根据get key的形式再次转为相应的对象类型
        List<Course> list = JSON.parseArray(map.get("course").toString(), Course.class);
        // 将课程编号、专业编号、开课时间放入放入培育计划类型集合中，进行批量插入
        List<CultivationPlan> cultivationPlans = new ArrayList<>();

        for (Course course : list) {
            CultivationPlan cultivationPlan = new CultivationPlan();
            cultivationPlan.setMajor_id((Integer) map.get("MajorID"));
            cultivationPlan.setCourse_id(course.getId());
            cultivationPlan.setStart_time(map.get("start_time").toString());
            cultivationPlans.add(cultivationPlan);
        }
        cultivationPlanService.AddCultivationPlan(cultivationPlans);
        return Result.SUCCESS;
    }

    @RequestMapping("deleteCultivationPlan")
    public Result deleteCultivationPlan(@RequestBody String data){
        // 将接收到的Json解析为map
        JSONObject jsonObject = JSONObject.parseObject(data);
        Map<String, Object> map = new HashMap<>();
        map.putAll(jsonObject);
        // 将map里的data进行toString后继续进行Json解析
        JSONObject jsonObject2 = JSONObject.parseObject(map.get("data").toString());
        Map<String, Object> map2= new HashMap<>();
        map2.putAll(jsonObject2);
        map2.get("course_id").toString();

        cultivationPlanService.DeleteCultivationPlan((Integer) map2.get("major_id"),map2.get("course_id").toString());
        return Result.DELETE_SUCCESS;
    }
}
